<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Model\System;
use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Support\Facades\Hash;


class Guest extends \App\Http\Controllers\Controller
{

    //前台登入
    public function login(Request $req)
    {

        $credentials = $req->only('email', 'password');
        $credentials['type'] = 0;
        $token =  auth()->attempt($credentials);
        if ($token) {
            return $this->resSuccess('登入成功', [
                'token'=> $token,
                'token_type'=> 'bearer',
                'expires_in' => auth()->factory()->getTTL() * 60,
            ]);
        }

        return $this->resFail('用户名或密码错误');
    }


    //前台用户注册
    public function reg(Request $req)
    {

        if (!$req->email || !$req->password) {
            return $this->resFail('err');
        }
        $user = User::where('email', $req->email)->count();

        if ($user) {
            return $this->resFail('失败，邮箱已经存在！');
        }
        $user = User::create([
            'type' => 0,
            'name' => $req->email,
            'email' => $req->email,
            'password' => Hash::make($req->password),
        ]);

        return $user ?
            $this->resSuccess('注册成功')
            : $this->resFail('操作失败');
    }



}
